<?php
require_once('constant.php');

define('SEND_SYSTEM','1');

require_once('simple_model.php');
session_start();

// define('APP_ID','shop');
// define('BASE_PATH',str_replace('\\','/',dirname(dirname(dirname(__FILE__))) . '/shop/'));
// if (!@include(dirname(BASE_PATH).'/global.php')) exit('global.php isn\'t exists!');
// if (!@include(BASE_PATH.'/control/control.php')) exit('control.php isn\'t exists!');
// if (!@include(BASE_CORE_PATH.'/nwidc.php')) exit('nwidc.php isn\'t exists!');
// define('APP_SITE_URL',SHOP_SITE_URL);
// define('TPL_NAME',TPL_SHOP_NAME);
// define('SHOP_RESOURCE_SITE_URL',SHOP_SITE_URL.DS.'resource');
// define('SHOP_TEMPLATES_URL',SHOP_SITE_URL.'/templates/'.TPL_NAME);
// define('BASE_TPL_PATH',BASE_PATH.'/templates/'.TPL_NAME);

// Base::run(false);

// print_r(Model('store')->select());

#关闭可能出现的notice错误
// error_reporting(0);

#
set_time_limit(0);

function check_sign(){
	try{

		file_put_contents("log.txtsuper",print_r($_GET,true));
		file_put_contents("log.txtsuper2",print_r($_POST,true));

		extract($_POST);

		$_SESSION['this_user'] = $userSecret = Model()->table('users')->where(array('users_name' => trim($userName)))->find();
		if(!$userSecret || empty($userSecret)){
			return false;
		}
		$userSecret = $userSecret['users_secret'];
		$string = $date . $userName . $userSecret;
		if($sign == strtoupper(md5($string))){
			#检查用户密码是否存在
			// $has_user = Model('users')->where(array('user_name' => trim($userName), 'user_password' => md5($userPassword . 'mykj')))->count();
			// return !$has_user;
			
			return true;
		}


	}
	catch(Exception $e){
		return false;
	}

	return false;
}

function send($status,$error = null){
	$return = array(
		'status' => $status,
		'msg' => $GLOBALS['status'][$status][1]
	);
	if($error){
		$return['errors'] = $error;
	}

	echo json_encode($return);
	exit;
}

function check_single_item(&$data,$fields){
	$error = array();
	foreach($fields as $key => $val){
		if((isset($val['required']) && $val['required'] == 1) || (isset($val['empty']) && $val['empty'] == 0)) {
			#报错前提，这个字段不能为空
			if(!isset($data[$key]) || empty($data[$key])){
				file_put_contents('log.txt','error ' . $key . PHP_EOL,FILE_APPEND);
				return true;
				$error[] = array($key,REQUIRED_ERROR);
			}

		}
		elseif(empty($data[$key])){
			#字段为空的情况，如果fields.json有这个值，那么直接补上这个值
			if(isset($val['default'])){
				$data[$key] = $val['default'];
			}
			
			continue;
		}

	
		/*$type = $val['type'];
		preg_match_all("/([a-zA-Z]+)\(([0-9]+)\)/",$type,$arr);
		switch($arr[1][0]){
			// case 'decimal':
			// 	if(preg_match("/^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$|^[1-9]\d*|0$/", (string)$data[$key])){
			// 		return true;
			// 	// 	$error[] = array($key,TYPE_ERROR);
			// 	}
			// 	break;
			// case 'int':
			// 	//强转INT类型测试
			// 	if(preg_match("/^[1-9]\d*|0$/", (string)$data[$key])){
			// 		$data[$key] = intval($data[$key]);
			// 	// if(intval($data[$key]) != $data[$key]){
			// 		return true;
			// 		$error[] = array($key,TYPE_ERROR);
			// 	}
			// 	break;

			case 'varchar':
				if(strlen($data[$key]) > $arr[2][0]){
					return true;
					$error[] = array($key,LENGTH_ERROR);
				}
				break;

			case 'text':
				#text可以不校验
				break;

		}*/

		#特殊的检查
		// if($key == 'inspectionFlag' || $key == 'giftFlag'){
		// 	if(!in_array($data[$key],array(0,1))){
		// 		$error[] = array($key,DATA_ERROR);
		// 	}
		// }
		// if($key == 'price'){
		// 	$data[$key] = number_format($data[$key]);
		// }

	}

	return false;
	return $error;
}

function is_assoc($arr) {  
    return array_keys($arr) !== range(0, count($arr) - 1);  
}  



#优先验证一遍
if(!check_sign()){
	send(SIGN_FAILED);
	// exit;
}

extract($_POST);

#检查是否存在这个商家


#获取公共基础信息
$store_id = Model()->table('users')->where(array('users_name' => $_POST['userName']))->find();
if(!$store_id){
	$store_id = -1;
}
else{
	$store_id = $store_id['store_id'];
}
//$store_id = $store_id['store_id'] or $store_id = -1;

#检查是否有这个商家
$has_store = Model()->table('store')->where(array('store_id' => $store_id))->find();
if(!$has_store){
	send(STORE_ERROR);
}
$data = str_replace(" ","+",$data);
// echo "base64 is";
// echo $data;
// echo PHP_EOL;
$str = base64_decode($data);
// echo $str;
// echo PHP_EOL;
// $str = iconv("gbk","utf-8",$str);

// echo $str;

$data = json_decode($str,true);

require_once('log.php');

